<!-- 用户等级产品 -->
<template>
    <div>
        <div class="table-page-search-wrapper">
            <a-form layout="inline">
                <a-row type="flex" justify="space-between" :gutter="48">
                    <a-col :md="4">
                        <a-form-item >
                            <a-button style="margin-right: 20px" type="primary" icon="plus" @click="add()">新增</a-button>
                        </a-form-item>
                    </a-col>
                </a-row>
            </a-form>
        </div>
        <a-table rowKey="id" :loading="isload" :columns="columns" :data-source="dataList" @change="handleTableChange" :pagination="{ pageSize: 10, total: total ,showSizeChanger: true,pageSize: size}" size="small">
            <!-- <div slot="storageSpace" slot-scope="storageSpace">
                <span>{{storageSpace || 0}}Tib</span>
            </div> -->
             <div slot="isOn" slot-scope="isOn">
                <a-switch checked-children="开" un-checked-children="关" disabled :checked="isOn == 1 ? true : false" />
            </div>
            <div slot="imgUrl" slot-scope="imgUrl">
                <img style="width:160px;height:80px" :src="imgUrl" alt="">
            </div>
            <template slot="operation" slot-scope="text, record">
                <div class="editable-row-operations">
                    <a-button type="link" @click="update(record)">修改</a-button>
                    <a-popconfirm
                        title="是否确认删除当前等级?"
                        ok-text="Yes"
                        cancel-text="No"
                        @confirm="del(record)"
                    >
                        <a-button type="link" class="errorColor" v-if="record.grade == dataList.length - 1">删除（注：只能从高往低删）</a-button>
                    </a-popconfirm>
                </div>
            </template>
        </a-table>

        <!-- 添加、修改等级产品 -->
        <a-modal
            title="添加、修改等级产品"
            :visible="visible"
            :confirm-loading="confirmLoading"
            @ok="handleOk"
            @cancel="visible = false"
            >
            <div>
                <a-form :label-col="{ span: 6 }" :wrapper-col="{ span: 17 }">
                    <a-form-item label="等级名称">
                        <a-input v-model="item.gradeName" placeholder="请输入等级名称" allow-clear></a-input>
                    </a-form-item>
                    <!-- <a-form-item label="升级所需算力">
                        <a-input v-model="item.storageSpace" placeholder="请输入升级所需算力" suffix="Tib" allow-clear></a-input>
                    </a-form-item> -->
                    <a-form-item label="升级描述">
                        <a-input v-model="item.upgradeDescription" type="textarea" placeholder="请输入升级描述" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="权益描述">
                        <a-input v-model="item.rebateDescription" type="textarea" placeholder="请输入权益描述" allow-clear></a-input>
                    </a-form-item>
                    <a-form-item label="图片描述">
                        <a-input v-model="item.imgUrl" placeholder="请输入描述图片链接" allow-clear></a-input>
                    </a-form-item>
                     <a-form-item label="是否展示">
                        <a-radio-group v-model="item.isOn" >
                            <a-radio :value="0">关闭</a-radio>
                            <a-radio :value="1">开启</a-radio>
                        </a-radio-group>
                    </a-form-item>
                </a-form>
            </div>
        </a-modal>
    </div>
</template>

<script>
import { queryGradeProduct , updateGradeProduct } from '@/api/product';
import { columns } from './table';
export default {
    data () {
        return {
            columns,
            brandId: this.global.brandId,
            brandList: [],
            dataList: [],
            isload: false,
            page: 1,
            size: 10,
            total: 0,
            grade: undefined,
            visible: false,
            confirmLoading: false,
            item: {
                id: '', 
                grade: '', // 等级（添加的时候只能添加最高级往上一级，不能随意添加等级）
                gradeName: '', // 等级名称
                // storageSpace: '', // 升级需要的算力，单位Tib
                upgradeDescription: '', // 升级描述
                rebateDescription: '', // 权益描述
                imgUrl: '', // 描述图片
                isOn:1
            },
              isHideList: [
                { id: 1, name: '是'},
                { id: 0, name: '否'},
            ],
        };
    },
    async created(){
        this.getGrade()
    },
    methods: {
        // 获取品牌等级产品
        getGrade(){
            let _this = this
            console.log(_this.brandId,'brandid')
            const params = {
                'brandId':_this.brandId,
                'grade':_this.grade,
                'page':_this.page - 1,
                'size':_this.size,
            }
            queryGradeProduct(params)
            .then(res=>{
                if(res.success){
                    _this.dataList = res.data.content
                    _this.total = res.data.totalElements
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        handleTableChange(pagination, filters, sorter) {
            console.log(pagination);
            this.page = pagination.current;
            this.size = pagination.pageSize;
            this.getGrade()
        },
        // 添加产品
        add(){
            if(!this.brandId){
                this.$message.warning('请先选择品牌')
                return
            }
            this.item.id = ''
            this.item.brandId = this.brandId
            this.item.grade = this.dataList.length
            this.item.gradeName = ''
            this.item.upgradeDescription = ''
            this.item.rebateDescription = ''
            this.item.imgUrl = ''
            this.item.isOn=1
            this.visible = true
        },
        // 修改产品
        update(item){
            this.item = JSON.parse(JSON.stringify(item))
            this.visible = true
        },
        // 确认添加、修改等级产品
        handleOk(){ 
            let _this = this
            const params = {
                'brandId':_this.item.brandId,
                'id':_this.item.id,
                'grade':_this.item.grade,
                // 'storageSpace': _this.item.storageSpace,
                'gradeName':_this.item.gradeName,
                'upgradeDescription':_this.item.upgradeDescription,
                'rebateDescription':_this.item.rebateDescription,
                'imgUrl':_this.item.imgUrl,
                'isOn':_this.item.isOn
            }
            updateGradeProduct(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false
                    _this.getGrade()
                    _this.$notification['success']({
                        message: '提示',
                        description: _this.item.id ? '修改成功' : '添加成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
        // 删除产品
        del(item){
            let _this = this
            const params = {
                'brandId':_this.brandId,
                'id':item.id,
                'grade':item.grade,
                'isDelete':1,
            }
            updateGradeProduct(params)
            .then(res=>{
                if(res.success){
                    _this.visible = false
                    _this.getGrade()
                    _this.$notification['success']({
                        message: '提示',
                        description: '删除成功',
                        duration: 8,
                    })
                }else{
                    _this.$notification['error']({
                        message: '提示',
                        description: res.errMessage,
                        duration: 8,
                    })
                }
            })
        },
    }
}

</script>
<style scoped>

</style>